package d.n.d.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import d.n.d.constant.Constant;
import d.n.d.domain.RespJSON;
import d.n.d.domain.Transaction;
import d.n.d.domain.User;
import d.n.d.service.AccountService;
import d.n.d.service.TransactionService;
import d.n.d.service.UserService;

/**
 * 
 * @author D.N.D
 * 
 */
@Controller
public class AdminController {

	@Autowired
	private TransactionService transactionService;

	@Autowired
	private AccountService accountService;

	@Autowired
	private UserService userService;

	private String message;
	private String error;

	/**
	 * <br> Author: DucDS </br>
	 * <br> Go to adminReviewTransaction Page. </br>
	 * 
	 * @param request
	 * @param model
	 * @return String
	 */
	@RequestMapping(value = "/adminReviewTransaction", method = RequestMethod.GET)
	public String doReviewTransaction(HttpServletRequest request, Model model) {

		// Get list transaction state Pending
		List<Transaction> listTransaction = transactionService
				.getTransactionListPending();
		if (listTransaction.size() > 0) {
			model.addAttribute("listTransaction", listTransaction);
		}
		return "reviewTransaction";
	}

	/**
	 * @author DucLV
	 * Confirm information after select APPROVE function.
	 * 
	 * @param request
	 * @param transactionId
	 * @return
	 */
	@RequestMapping(value = "/approveTransaction", method = RequestMethod.GET)
	public @ResponseBody RespJSON approveTransaction(
			HttpServletRequest request,
			@RequestParam("transactionId") String transactionId) {

		return transactionService.confirmInfoApproveTransaction(transactionId);
	}

	/**
	 * @author DucLV
	 * Execute APPROVE into Database.
	 * 
	 * @param request
	 * @param transactionId
	 * @param transactionComment
	 * @return JSON object
	 */
	@RequestMapping(value = "/approveTransactionComplete", method = RequestMethod.POST)
	public @ResponseBody RespJSON approveTransactionComplete(
			HttpServletRequest request,
			@RequestParam("idTransactionApr") String transactionId,
			@RequestParam("comment") String transactionComment) {

		User userAdmin = (User) request.getSession().getAttribute("user");

		return transactionService.executeApproveTransaction(transactionId, transactionComment, userAdmin);
	}

	/**
	 * 
	 * @param request
	 * @param transactionId
	 * @return
	 */
	@RequestMapping(value = "/rejectTransaction", method = RequestMethod.GET)
	public @ResponseBody RespJSON rejectTransaction(HttpServletRequest request,
			@RequestParam("transactionId") String transactionId) {
		String validation = transactionService.verifyTransaction(transactionId);
		if (validation.equals("")) {
			message = Constant.TRANSACTION_VALID;
			error = null;
		} else {
			error = Constant.TRANSACTION_INVALID;
			message = null;
		}
		Transaction transaction = transactionService
				.findTransactionById(Integer.parseInt(transactionId));
		if (transaction.getSendAccount() != null) {
			transaction.getSendAccount().getIdCardNumber().setAccounts(null);
		}
		if (transaction.getReceiveAccount() != null) {
			transaction.getReceiveAccount().getIdCardNumber().setAccounts(null);
		}
		if (transaction.getUserSupport() != null) {
			transaction.getUserSupport().setTransactionUserAdmin(null);
			transaction.getUserSupport().setTransactionUserSupport(null);
		}

		RespJSON respJSON = new RespJSON(message, error, transaction,
				validation);
		return respJSON;
	}

	/**
	 * 
	 * @param request
	 * @param model
	 * @param transactionId
	 * @param transactionComment
	 * @return
	 */
	@RequestMapping(value = "/rejectTransactionComplete", method = RequestMethod.POST)
	public @ResponseBody RespJSON rejectTransactionComplete(
			HttpServletRequest request, Model model,
			@RequestParam("idTransactionReject") String transactionId,
			@RequestParam("comment") String transactionComment) {

		User userAdmin = (User) request.getSession().getAttribute("user");
		if (transactionComment.length() > 30) {
			transactionComment = transactionComment.substring(0, 30);
		}
		if (transactionService.reject(transactionId, userAdmin, transactionComment)) {
			message = "Transaction Id = " + transactionId + " is rejected!";
			error = null;
		} else {
			error = Constant.ERROR_SQL;
			message = null;
		}
		String validation=null;
		Transaction transaction = transactionService
				.findTransactionById(Integer.parseInt(transactionId));

		if (transaction.getSendAccount() != null) {
			transaction.getSendAccount().getIdCardNumber().setAccounts(null);
		}
		if (transaction.getUserSupport() != null) {
			transaction.getUserSupport().setTransactionUserAdmin(null);
			transaction.getUserSupport().setTransactionUserSupport(null);
		}

		if (transaction.getReceiveAccount() != null) {
			transaction.getReceiveAccount().getIdCardNumber().setAccounts(null);
		}
		if (transaction.getUserAdmin() != null) {
			transaction.getUserAdmin().setTransactionUserAdmin(null);
			transaction.getUserAdmin().setTransactionUserSupport(null);
		}
		RespJSON respJSON = new RespJSON(message, error, transaction,
				validation);
		return respJSON;
	}

	/**
	 * </br> Go to adminViewTransaction Page. </br>
	 * 
	 * @param request
	 * @param model
	 * @return String
	 */
	@RequestMapping(value = "/adminViewTransaction", method = RequestMethod.GET)
	public String doViewTransaction(HttpServletRequest request, Model model) {

		String transactionId = request.getParameter("transactionId");
		String verify = transactionService.verifyTransaction(transactionId);
		if (verify.equals(Constant.ERROR_PARSE_INT)
				|| verify.equals(Constant.ERROR_TRANSACTION_NOT_FOUND)) {
			model.addAttribute("error", verify);
			return "adminViewTransactionPage";
		}
		model.addAttribute("transaction", transactionService
				.findTransactionById(Integer.parseInt(transactionId)));
		return "adminViewTransactionPage";
	}
}
